package com.nuts.core.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nuts.base.bean.ResultVO;
import com.nuts.base.core.ResultGenerator;
import com.nuts.base.core.annotation.Permission;
import com.nuts.base.core.annotation.PermissionPath;
import com.nuts.core.bean.request.RoleReqVO;
import com.nuts.core.bean.response.RoleVO;
import com.nuts.core.service.IRoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 角色控制器
 * 
 * @author 许自辉
 * @date 2017年8月2日
 */
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/角色")
@Api(value = "role-api", tags = { "角色相关接口" })
public class RoleController {

	@Autowired
	private IRoleService roleService;

	@ApiOperation("分页查询角色")
	@Permission("role:findPage")
	@GetMapping("role/findPage")
	public ResultVO getRolePage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			RoleReqVO roleReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		return ResultGenerator.getSuccess(new PageInfo<RoleVO>(roleService.getList(roleReqVO)));
	}
	
	@ApiOperation("查询角色")
	@Permission("role:findList")
	@GetMapping("role/findList")
	public ResultVO getRoleList(RoleReqVO roleReqVO) {
		return ResultGenerator.getSuccess(roleService.getList(roleReqVO));
	}

	@ApiOperation("添加角色")
	@Permission("role:create")
	@PostMapping("role/create")
	public ResultVO createRole(@RequestBody RoleReqVO roleReqVO) {
		int count = roleService.insert(roleReqVO);
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新角色")
	@Permission("role:update")
	@PutMapping("role/update")
	public ResultVO updateRole(@RequestBody RoleReqVO roleReqVO) {
		int count = roleService.update(roleReqVO);
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除角色")
	@Permission("role:delete:id")
	@DeleteMapping("role/delete/{id}")
	public ResultVO deleteRoleById(@ApiParam(value = "角色id", required = true) @PathVariable("id") Integer id) {
		int count = roleService.deleteById(id);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
